package com.example.androidlesson;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

    private FragmentManager fragmentManager;

    private RadioGroup navBar;
    private RadioButton selectedButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getSupportFragmentManager();

        // 把自身设置为Listener，实现onCheckedChanged
        navBar = findViewById(R.id.nav_bar);
        navBar.setOnCheckedChangeListener(this);
        selectedButton = findViewById(R.id.info_button);
        selectedButton.setChecked(true);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment;
        switch (checkedId){
            case R.id.info_button:
                fragment=InfoFragment.GetInfoFragment();
                break;
            case R.id.fruit_button:
                fragment=FruitFragment.GetFruitFragment();
                break;
            case R.id.my_button:
                fragment=MyFragment.GetMyFragment();
                break;
            case R.id.setting_button:
                fragment=SettingFragment.GetSettingFragment();
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + checkedId);
        }
        fragmentTransaction.replace(R.id.fragContainer,fragment);
        fragmentTransaction.commit();
    }

}